/*
 * $RCSfile: Pocer.java,v $
 * $Revision: 1.1  $
 * $Date: 2012-8-13  $
 *
 * Copyright (C) 2005 Bettem, Inc. All rights reserved.
 *
 * This software is the proprietary information of Bettem, Inc.
 * Use is subject to license terms.
 */
package com.dzpk.gameserver.module.main.pocer;

import java.util.Random;

import com.dzpk.gameserver.module.main.RobotPock;
import org.apache.log4j.Logger;


/**
 * 扑克对象 开始游戏则new一个该对象
 * <p>Title: Pocer</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2006</p>
 * @author shenxing.ruan
 * @version 1.0
 */
public class PocerLink {
	public static Logger logger = Logger.getLogger(PocerLink.class);
	private static Pocer[] p_ = new Pocer[52];
	private Random r = new Random();
	/** 52张牌顺序 */
	private int pocerArray[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51};
	private int index = 0; //当前第几张牌了 重0开始计算
	/**
	 * 0皇家同花 1同花 2四条 3葫芦4同花 5顺子 6三条 7两对 8一对 9高牌
	 */
	public static int POCER_TYPE[] = {1,2,3,4,5,6,7,8,9,10};
	public int[] zuidapai;
	public int robotIndex = 0;

	private int s1 = -1;
	private int s2 = -1;
	private long userId;
	public PocerLink() {
//		shuffle();
	}

	/**
	 * 1 获取 1-2张牌 3发两张牌是获取正常牌 2获取公共牌
	 * @param type
	 * @return
	 */
	public Pocer robotNext(int type) {
		if (userId < 0) {
			return next();
		}
		Pocer pocer = null;
		if (type == 1) {
			robotIndex ++;
			if (robotIndex > 2) {
				logger.error("robotNext err!!!!");
			}
			return new Pocer(zuidapai[robotIndex -1]);
		}else if (type == 2) {
			if ((robotIndex == 2 && s1 == -1) || (robotIndex == 3 && s2 == -1) || robotIndex >= 5) {
				s1 = 1;
				if (robotIndex == 3) {
					s2 = 1;
				}
				int w = 0;
				while(w == 0) {
					pocer = next();
					if (zuidapai[0] != pocer.getSize1() &&zuidapai[1] != pocer.getSize1() &&zuidapai[2] != pocer.getSize1() && zuidapai[3] != pocer.getSize1() && zuidapai[4] != pocer.getSize1()) {
						w = 1;
						return pocer;
					}
				}
			}else {
				pocer = new Pocer(zuidapai[robotIndex]);
				robotIndex ++;
				return pocer;
			}
		}else if (type == 3) {
			int w = 0;
			while(w == 0) {
				pocer = next();
				if (zuidapai[0] != pocer.getSize1() &&zuidapai[1] != pocer.getSize1() &&zuidapai[2] != pocer.getSize1() && zuidapai[3] != pocer.getSize1() && zuidapai[4] != pocer.getSize1()) {
					w = 1;
					return pocer;
				}
			}
		}
		return null;
	}
	/**
	 * 依次获取一张牌
	 * @return
	 */
	private Pocer next() {
		Pocer pocer = p_[pocerArray[index]];
		index ++;
		return pocer;
	}


	/**
	 * 查看当前在第几张牌
	 * @return
	 */
	public int getIndex() {
		return index;
	}
	/**
	 * 洗牌
	 */
	public void shuffle(long userId) {
		s1 = -1;
		s2 = -1;
		robotIndex = 0;
		//

		index = 0;
		for (int i = 0 ; i < 52 ;i ++) {
			int a = r.nextInt(51);
			int c = pocerArray[a];
			pocerArray[a] = pocerArray[i];
			pocerArray[i] = c;
		}
		for (int i = 0 ; i < 30 ;i ++) {
			int a = r.nextInt(51);
			int b = r.nextInt(51);
			int c = pocerArray[a];
			pocerArray[a] = pocerArray[b];
			pocerArray[b] = c;
		}
		if (userId > 0) {
			this.userId = userId;
//			int[] ints = {1,2,3,4,5};
			zuidapai = RobotPock.getZuidapai();
			logger.debug(userId + " zui da pai is :" + zuidapai[0] + " " + zuidapai[1]+ " " + zuidapai[2]+ " " + zuidapai[3]+ " " + zuidapai[4]);
		}else {
			this.userId = -1;
		}
	}
	static {
		for (int i=0 ; i < p_.length ; i++) {
			p_[i] = new Pocer(i);
		}
	}

	public static void main(String[] args) {
		PocerLink pocerLink = new PocerLink();
		pocerLink.shuffle(12200);
		Pocer p111 = pocerLink.robotNext(3);
		Pocer p222 = pocerLink.robotNext(3);
		Pocer p1 = pocerLink.robotNext(1);
		Pocer p2 = pocerLink.robotNext(1);

		Pocer p11 = pocerLink.robotNext(3);
		Pocer p22 = pocerLink.robotNext(3);

		Pocer p3 = pocerLink.robotNext(2);
		Pocer p4 = pocerLink.robotNext(2);
		Pocer p5 = pocerLink.robotNext(2);
		Pocer p6 = pocerLink.robotNext(2);
		Pocer p7 = pocerLink.robotNext(2);

		System.out.println(p1.getSize1() + "/" + p1.getSize2());
		System.out.println(p2.getSize1() + "/" + p2.getSize2());
		System.out.println(p3.getSize1() + "/" + p3.getSize2());
		System.out.println(p4.getSize1() + "/" + p4.getSize2());
		System.out.println(p5.getSize1() + "/" + p5.getSize2());
		System.out.println(p6.getSize1() + "/" + p6.getSize2());
		System.out.println(p7.getSize1() + "/" + p7.getSize2());
	}
}


